package org.example.demo1startjava17.filter;


import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * 配置方式1
 * 1.实现Filter接口
 * 2.添加注解@WebFilter(urlPatterns = "/*")
 * 3.application添加注解@ServletComponentScan
 * // TODO 此处添加order不起作用 vash
 * // TODO 如果设置了context-path，实际拦截的请求是 /context-path/{以下的url} vash
 * 关于@webFilter使用@Order无效问题：https://www.cnblogs.com/okong/p/correct-webfilter.html
 * // TODO 如果为了使用order加上component注解，则相当于添加了两次filter，还是使用filter3方式 vash
 */
@Slf4j
// TODO 如果设置了context-path，实际拦截的请求是 /context-path/{以下的url} vash
@WebFilter(urlPatterns = "/1/test/*")
public class MyFilter1 implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
//        log.info("MyFilter 初始化了");
        System.err.println("MyFilter1 初始化了");
        Filter.super.init(filterConfig);
//        System.err.println(filterConfig.getFilterName());
//        System.err.println(JSON.toJSONString(filterConfig.getServletContext()));
//        System.err.println(JSON.toJSONString(filterConfig.getInitParameterNames()));
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
//        log.info("MyFilter 执行了 {}", httpServletRequest.getRequestURI());
        System.err.println("MyFilter1 执行了 " + httpServletRequest.getRequestURI());
        filterChain.doFilter(servletRequest, servletResponse);
    }

}
